const fs = require('fs');
const path = require('path');

const Upload = async ctx => {
  let file = ctx.request.files.upfile;
  let filename = file.path.match(/(upload_[a-z0-9.]+)$/)[1];
  
  // const reader = fs.createReadStream(file.path);
  // let filename = Date.now() + '.' + file.name.split('.')[1];
  // let filePath = path.join(__dirname, '../public/upload/') + filename;
  // const stream = fs.createWriteStream(filePath);
  // reader.pipe(stream);

  ctx.response.body= {
    err: 0,
    desc: '上传成功！',
    path: '/public/upload/'+filename
  };
}

module.exports = {
  'POST /upload': Upload,
}